package com.expert.service.Impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.expert.Mapper.TitleMapper;
import com.expert.context.BaseContext;
import com.expert.dto.TitleDTO;
import com.expert.exception.DeleteException;
import com.expert.pojo.Expert;
import com.expert.pojo.Title;
import com.expert.pojo.User;
import com.expert.service.ExpertService;
import com.expert.service.TitleService;
import com.expert.service.UserService;
import com.expert.vo.TitleVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @ClassName: ExpertServiceImpl
 * @Author: cxs
 * @Date: 2024/03/14 13:08
 * @Description:
 **/
@Service
public class TitleServiceImpl extends ServiceImpl<TitleMapper,Title> implements TitleService {

    @Autowired
    UserService userService;

    @Autowired
    ExpertService expertService;

    /**
     * 分页查询职称
     *
     * @param page
     * @param pageSize
     * @param titleName
     * @param titleLevel
     * @return
     */
    @Override
    public Page<TitleVo> pageTitle(Integer page, Integer pageSize, String titleName, Integer titleLevel) {
        Page<Title>  titlePage=new Page<>(page,pageSize);
        page(titlePage,new LambdaQueryWrapper<Title>()
                .like(StringUtils.isNotEmpty(titleName),Title::getTitleName,titleName)
                .eq(titleLevel!=null,Title::getTitleLevel,titleLevel)
                .orderByDesc(Title::getCreateTime)
        );
        Page<TitleVo> titleVoPage=new Page<>();
        BeanUtils.copyProperties(titlePage,titleVoPage);
        List<TitleVo> titleVoList = titlePage.getRecords().stream().map(item -> {
            TitleVo titleVo = new TitleVo();
            BeanUtils.copyProperties(item, titleVo);
            User user = this.userService.getById(item.getCreateUser());
            titleVo.setCreateUserName(user.getName());
            return titleVo;
        }).collect(Collectors.toList());
        titleVoPage.setRecords(titleVoList);
        return titleVoPage;
    }

    @Override
    public void addTitle(TitleDTO title1) {
        Title title=new Title();
        BeanUtils.copyProperties(title1,title);
        title.setCreateTime(LocalDateTime.now());
        title.setUpdateTime(LocalDateTime.now());
        title.setCreateUser(BaseContext.getCurrentId());
        title.setUpdateUser(BaseContext.getCurrentId());
        this.save(title);
    }

    @Override
    @Transactional
    public void deleteTitles(List<Long> ids) {
        ids.forEach(id->{
            Long count = this.expertService.lambdaQuery().eq(Expert::getExpertTitle, id).count();
            if (count>0){
                throw  new DeleteException("职称下已存在专家信息，删除失败");
            }
        });
        this.removeBatchByIds(ids);
    }
}
